#region ... Copyright Notice ...
/*
   Copyright 2006-2008 Provo Labs    http://www.provolabs.com

	Author: Tyler Jensen    http://www.tsjensen.com/blog
	Released by the author with written permission of Provo Labs

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
#endregion

using System;
using System.Collections.ObjectModel;
using System.Text;

namespace Atrax.Html
{
	/// <summary>
	/// This collection provides easy access to tags using the Slice.Index as a key. Standard zero based sequential indexed 
	/// access can be achieved by casting this collection as System.Collections.ObjectModel.Collection.
	/// </summary>
	public class HtmlTagCollection : KeyedCollection<int, HtmlTag>
	{
		public HtmlTagCollection() : base() { }

		protected override int GetKeyForItem(HtmlTag item)
		{
			return item.Slice.Index;
		}

		public HtmlTag Next(HtmlTag tag)
		{
			if (tag == null) return null;
			if (!this.Contains(tag.Slice.Index)) throw new IndexOutOfRangeException("The tag does not exist in the collection.");
			int i = this.IndexOf(tag) + 1;
			if (i > 0 && i < this.Count)
				return ((Collection<HtmlTag>)this)[i];
			else
				return null;
		}

		public HtmlTag Next(HtmlTag tag, DtdElement element)
		{
			if (tag == null) return null;
			if (!this.Contains(tag.Slice.Index)) throw new IndexOutOfRangeException("The tag does not exist in the collection.");
			int i = this.IndexOf(tag) + 1;
			if (i < 0 || i >= this.Count) return null;

			bool found = false;
			while (i < this.Count)
			{
				if (((Collection<HtmlTag>)this)[i].Element == element)
				{
					found = true;
					break;
				}
				i++;
			}

			if(found)
				return ((Collection<HtmlTag>)this)[i];
			else
				return null;
		}

		public HtmlTag Previous(HtmlTag tag)
		{
			if (tag == null) return null;
			if (!this.Contains(tag.Slice.Index)) throw new IndexOutOfRangeException("The tag does not exist in the collection.");
			int i = this.IndexOf(tag) - 1;
			if (i > -1 && i < this.Count)
				return ((Collection<HtmlTag>)this)[i];
			else
				return null;
		}

		public HtmlTag Previous(HtmlTag tag, DtdElement element)
		{
			if (tag == null) return null;
			if (!this.Contains(tag.Slice.Index)) throw new IndexOutOfRangeException("The tag does not exist in the collection.");
			int i = this.IndexOf(tag) - 1;
			if (i < 0 || i >= this.Count) return null;

			bool found = false;
			while (i > -1)
			{
				if (((Collection<HtmlTag>)this)[i].Element == element)
				{
					found = true;
					break;
				}
				i--;
			}

			if (i > -1 && i < this.Count)
				return ((Collection<HtmlTag>)this)[i];
			else
				return null;
		}

	}
}
